package lock;

import buyTicket.BuyThread;

import java.util.Locale;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Buy extends Thread{
    static int ticket = 0;
    static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true){
                lock.lock();
                try {
                    if(ticket < 100){

                        Thread.sleep(100);

                        ticket++;
                        System.out.println(getName() + "正在买第" + ticket +"张票");
                    }else {
                        break;
                    }
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }finally {
                    lock.unlock();
                }

        }
    }
}
